package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.myException.ServiceException;
import com.wn.common.pojo.Order;
import com.wn.common.pojo.Rsp;
import com.wn.common.pojo.Traveller;
import com.wn.common.pojo.User;
import com.wn.mapper.TravellerMapper;
import com.wn.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
@Slf4j
public class TravellerService {
       @Autowired
       private TravellerMapper travellerMapper;


    /**
     * 添加
     * @param traveller
     * @return
     */
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
        public Rsp insertTraveller(Traveller traveller){
            if(traveller.getUser()==null) {
                int exe = travellerMapper.insert(traveller);
                if (exe > 0)
                    return Rsp.success(null).msg("添加成功！");
                else
                    return Rsp.fail().msg("添加失败！");
            }
            else {
                int exe1 = travellerMapper.insert(traveller);
                int exe2 = travellerMapper.insertTravellerUser(traveller.getTravellerId(), traveller.getUser().getUserId(), traveller.getCreateUserId());
                if((exe1&exe2)>0)
                    return Rsp.success(null).msg("添加成功!");
                else
                    return Rsp.fail().msg("添加失败！");
            }
        }


    /**
     * 修改
     * @param traveller
     * @return
     */
    public Rsp updateTraveller(Traveller traveller){

            int exe =  travellerMapper.update(traveller);
            if(exe>0)
                return Rsp.success(null).msg("修改成功！");
            else
                return Rsp.fail().msg("修改失败！");
        }

    /**
     * 删除游客
     * @param travellerId
     * @return
     */

    @Transactional
    public Rsp deleteTraveller(int travellerId,int updateUserId){
       int exe1 =  travellerMapper.deleteTravellerUserByTravellerId(travellerId);
       int exe2 = travellerMapper.deleteByPrimaryKey(updateUserId,travellerId);
       if((exe1&exe2)>0){
           return Rsp.success(null).msg("删除成功！");
       }
       else
           return Rsp.fail().msg("删除失败!");
        }



    /**
     * 根据主键查询游客
      * @param travellerId
     * @return
     */
        public Rsp selectTravellerByPrimaryKey(int travellerId) {
            Traveller traveller = travellerMapper.selectByPrimaryKey(travellerId);
            if (traveller != null)
                return Rsp.success(traveller).msg("查询成功！");

            else
                return Rsp.fail().msg("查询失败");
        }
    /**
     * 查询游客列表
     * @param page
     * @param pageSize
     * @return
     */
        public Rsp selectTravellerList(Integer page,Integer pageSize){
            PageHelper.startPage(page,pageSize);
            List<Traveller> travellers = travellerMapper.selectByCondition(null);
            PageInfo pageInfo = new PageInfo(travellers);
            if(pageInfo.getList()!=null)
                return  Rsp.success(pageInfo).msg("查询成功！");
            else
                return Rsp.fail().msg("查询失败！");
        }

    /**
     * 根据条件查询游客列表
     * @param page
     * @param pageSize
     * @param traveller
     * @return
     */
    public Rsp selectTravellerListByCondition(Integer page,Integer pageSize,@RequestBody Traveller traveller){
        PageHelper.startPage(page,pageSize);
        List<Traveller> travellers = travellerMapper.selectByCondition(traveller);
        PageInfo pageInfo =  new PageInfo(travellers);
        if(pageInfo.getList()!=null)
            return Rsp.success(pageInfo).msg("查询成功！");
        else
            return Rsp.fail().msg("查询失败！");
    }

    /*根据订单中的旅客信息为旅客表增加旅客*/
    public Order insertTravellerByOrder(Order order) {
        try {
            for(Traveller t: order.getTravellers()){
                travellerMapper.insertTravellerByOrder(t);
                log.info("旅客增加后返回主键：{}",t);
                //根据订单主键和旅客主键添加中间表
                if(order.getId() == null){
                    throw new ServiceException("订单主键为空");
                }
                Integer insertResult = travellerMapper.insertTempByTravellerIdAndOrderId(t.getTravellerId(),
                        order.getId());
                if(insertResult != 1){
                    throw new ServiceException();
                }
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return order;

    }
}
